﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace Yeex.Boss.Web.Helper
{
    public static class FormHelper
    {
        public static MvcForm BeginJuiAjaxForm(this HtmlHelper htmlHelper)
        {
            return htmlHelper.BeginJuiForm(null, null, FormMethod.Post, new { onsubmit = "return validateCallback(this,navTabAjaxDone_Deal)" });
        }

        public static MvcForm BeginJuiAjaxForm(this HtmlHelper htmlHelper, object routeValues)
        {
            return htmlHelper.BeginJuiForm(null, null, routeValues, FormMethod.Post, new { onsubmit = "return validateCallback(this,navTabAjaxDone_Deal)" });
        }

        public static MvcForm BeginJuiAjaxForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, routeValues, FormMethod.Post, new { onsubmit = "return validateCallback(this,navTabAjaxDone_Deal)" });
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper)
        {
            return htmlHelper.BeginJuiForm(null);
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, object routeValues)
        {
            return htmlHelper.BeginJuiForm(new RouteValueDictionary(routeValues));
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues)
        {
            return htmlHelper.BeginJuiForm(null, null, routeValues);
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, FormMethod.Post);
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, method, null);
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, new RouteValueDictionary(routeValues), FormMethod.Post);
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, routeValues, FormMethod.Post);
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, method, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, IDictionary<string, object> htmlAttributes)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, null, method, htmlAttributes);
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, new RouteValueDictionary(routeValues), method);
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, routeValues, method, null);
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method, object htmlAttributes)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, routeValues, method, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes)
        {
            return htmlHelper.BeginJuiForm(actionName, controllerName, new RouteValueDictionary(routeValues), method, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        /// <summary>
        /// 创建Form
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="actionName"></param>
        /// <param name="controllerName"></param>
        /// <param name="routeValues"></param>
        /// <param name="method"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcForm BeginJuiForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes)
        {
            if (htmlAttributes == null)
            {
                htmlAttributes = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
                htmlAttributes.Add("class", "pageForm required-validate");
                htmlAttributes.Add("onsubmit", "return validateCallback(this,navTabAjaxDone)");
            }
            else
            {
                if (htmlAttributes.ContainsKey("class"))
                {
                    htmlAttributes["class"] += "pageForm required-validate";
                }
                else
                {
                    htmlAttributes.Add("class", "pageForm required-validate");
                }
                if (!htmlAttributes.ContainsKey("onsubmit"))
                /*{
                    htmlAttributes["onsubmit"] += " return validateCallback(this,navTabAjaxDone)";
                }
                else*/
                {
                    htmlAttributes.Add("onsubmit", "return validateCallback(this,navTabAjaxDone)");
                }
            }

            return htmlHelper.BeginForm(actionName, controllerName, routeValues, method, htmlAttributes);
        }
    }
}